﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using System.Threading;
using System.Windows.Forms;
using EDBuilder;

namespace WebDictionary
{
    class Pinyinutil
    {
         StringDictionary PinyinDictionary;
        public bool Initialize_done;

        public Pinyinutil()
        {
        }
        public bool InitPinyinTable()
        {
            Initialize_done = false;
          
            //Thread InitPinyinThread = new Thread(Pinyinutil.InitPinyinTableWorker);
            //InitPinyinThread.Start();
            return InitPinyinTableWorker();

        }

        private bool InitPinyinTableWorker()
        {
            StringReader sr = null;
            try
            {
                PinyinDictionary = new StringDictionary();
                sr = new StringReader(EDBuilder.Properties.PinyinData.strPinYin);
                int KanjiCol = 0; // Column of Kanji
                int PinyinCol = 2; // Column of Pinyin
                int ToneCol = 3;   // Column of Tone

                string line;
                while (null != (line = sr.ReadLine()))
                {
                    string[] columns;
                    string Kanji;
                    string Pinyin;
                    char[] separator = "\t".ToCharArray();

                    columns = line.Split(separator);
                    Kanji = columns[KanjiCol];
                    Pinyin = columns[PinyinCol] + columns[ToneCol];

                    //try
                    //{
                    if (!PinyinDictionary.ContainsKey(Kanji))
                        PinyinDictionary.Add(Kanji, Pinyin);
                    else
                        PinyinDictionary[Kanji] = PinyinDictionary[Kanji] + "\t" + Pinyin;
                    //}
                    //catch (ArgumentException) // duplicate key
                    //{ }  // just ignore
                }
                Initialize_done = true;
                if (sr != null)
                    sr.Close();
                
                return Initialize_done;
            }
            catch (Exception eee)
            {
                Initialize_done = false;
                if (sr != null)
                    sr.Close();
                return false;
                
            }
            
        }
        public string GetPinyin(string srcstr)
        {
            string deststr = "";
            
            char[] separator = "\t".ToCharArray();
            if (!Initialize_done)
            {
                return "";
            }
            for (int i = 0; i < srcstr.Length; i++)
            {
                if (!PinyinDictionary.ContainsKey(srcstr[i].ToString()))
                {
                    return "";
                }
                string[] pinyins = PinyinDictionary[srcstr[i].ToString()].Split(separator);
                if (pinyins.Length <= 0)
                {
                    return "";
                }
                deststr += " " + pinyins[0]; 

            }
            return deststr.Trim();
        }

        public bool IsValidChineseChar(char cw)
        {
            return PinyinDictionary.ContainsKey(cw.ToString());
        }

        public bool IsValidChineseWord(string cw)
        {
            for (int i = 0; i < cw.Length; i++)
            {
                if (!IsValidChineseChar(cw[i]))
                    return false;
            }
            return true;
        }

        public string GetAllPinyinInString(string srcstr)
        {
            char[] separator = "\t".ToCharArray();
            if (!Initialize_done)
            {
                return "";
            }

            if (!PinyinDictionary.ContainsKey(srcstr[0].ToString()))
            {
                return "";
            }
            return PinyinDictionary[srcstr];
        }

        public string[] GetAllPinyin(string srcstr)
        {
            string[] deststr = new string[100];

            string[] deststr2 = new string[100];
            char[] separator = "\t".ToCharArray();
            if (!Initialize_done)
            {
                throw new Exception("PinYin Table not loaded!");
            }
            int Len1 = 0;
            for (int i = 0; i < srcstr.Length; i++)
            {
                if (!PinyinDictionary.ContainsKey(srcstr[i].ToString()))
                {
                    return null;
                }
                string[] pinyins = PinyinDictionary[srcstr[i].ToString()].Split(separator);

                int Len2 = 0;
                for (int j = 0; j < pinyins.Length; j++)
                {
                    if (Len1 == 0)
                    {
                        deststr2[Len2++] = pinyins[j];
                    }
                    else
                    {
                        for (int k = 0; k < Len1; k++)
                        {
                            deststr2[Len2++] = deststr[k] + " " + pinyins[j];
                        }
                    }
                }
                deststr2.CopyTo(deststr, 0);
                Len1 = Len2;

            }
            return deststr;
        }
    }
}
